﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JT_C_JLB
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //InputLanguage.CurrentInputLanguage;
            textBox6_data.ImeMode = System.Windows.Forms.ImeMode.Disable;
        }

        private void textBox_input_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
                return;
            e.Handled = true;
        }

        private void textBox6_data_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue != 13)
                return;
            label_Display.Text = "we are family!";
        }

        private void textBox6_data_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue != 13)
                return;

            textBox6_data.SelectAll();

            string[] subStr = textBox6_data.Text.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            string strWhole = "";

            for (int i = 0; i < subStr.Length; i++)
            {
                if (subStr[i].Length % 2 != 0)
                    strWhole += "0";

                strWhole += subStr[i];
            }

            if (strWhole.Length != 8 && strWhole.Length != 14 && strWhole.Length != 12)
            {
                label_Display.Text = "Error Input length!example:2017 11 02 08 12 05 " + DateTime.Now.Hour.ToString() + ":"
                                             + DateTime.Now.Minute.ToString() + ":"
                                             + DateTime.Now.Second.ToString();
                return;
            }
            if (strWhole.Length == 8)
            {
                #region time32 转 标准
                byte[] data = new byte[4];
                for (int i = 0; i < 4; i++)
                {
                    try
                    {
                        data[i] = byte.Parse(strWhole.Substring(2 * i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                    }
                    catch (Exception)
                    {
                        label_Display.Text = "Error Input code! " + DateTime.Now.Hour.ToString() + ":"
                                                     + DateTime.Now.Minute.ToString() + ":"
                                                     + DateTime.Now.Second.ToString();
                        return;
                    }
                }
                UInt32 time32 = ((UInt32)data[3] << 24) +
                                    ((UInt32)data[2] << 16) +
                                    ((UInt32)data[1] << 8) +
                                    ((UInt32)data[0] << 0);
                string year = ((time32 >> (32 - 6)) + 2000).ToString();
                time32 <<= 6;
                string mon = (time32 >> (32 - 4)).ToString();
                time32 <<= 4;
                string day = (time32 >> (32 - 5)).ToString();
                time32 <<= 5;
                string hour = (time32 >> (32 - 5)).ToString();
                time32 <<= 5;
                string min = (time32 >> (32 - 6)).ToString();
                time32 <<= 6;
                string sec = (time32 >> (32 - 6)).ToString();
                time32 <<= 6;

                label_Display.Text = year + "年" + mon + "月" + day + "日" + hour + "时" + min + "分" + sec + "秒";
                #endregion
            }
            else
            {
                if (strWhole.Length == 14)
                    strWhole = strWhole.Substring(2);

                UInt32 time32 = 0;

                try
                {
                    UInt32 year = UInt32.Parse(strWhole.Substring(0, 2));
                    UInt32 mon = UInt32.Parse(strWhole.Substring(2, 2));
                    UInt32 day = UInt32.Parse(strWhole.Substring(4, 2));
                    UInt32 hour = UInt32.Parse(strWhole.Substring(6, 2));
                    UInt32 min = UInt32.Parse(strWhole.Substring(8, 2));
                    UInt32 sec = UInt32.Parse(strWhole.Substring(10, 2));

                    time32 += (year);
                    time32 <<= 4;
                    time32 += (mon);
                    time32 <<= 5;
                    time32 += (day);
                    time32 <<= 5;
                    time32 += (hour);
                    time32 <<= 6;
                    time32 += (min);
                    time32 <<= 6;
                    time32 += (sec);
                    label_Display.Text =  string.Format("{0:X}", time32 << 24 >> 24).PadLeft(2, '0') + " " +
                                          string.Format("{0:X}", time32 << 16 >> 24).PadLeft(2, '0') + " " +
                                          string.Format("{0:X}", time32 << 8 >> 24).PadLeft(2, '0') + " " +
                                          string.Format("{0:X}", time32 << 0 >> 24).PadLeft(2, '0');
                    Clipboard.SetText(label_Display.Text);
                }
                catch (Exception)
                {
                    label_Display.Text = "Error Input code! " + DateTime.Now.Hour.ToString() + ":"
                                                     + DateTime.Now.Minute.ToString() + ":"
                                                     + DateTime.Now.Second.ToString();
                    return;
                }
                

            }
        }

        private void textBox6_data_TextChanged(object sender, EventArgs e)
        {
            byte[] dataBuf = hexStringToArray(textBox6_data.Text);
            label_Display.Text = "(" + DateTime.Now.ToString() + ") ";

            if (dataBuf == null)
            {
                label_Display.Text += "String unknow";
                return;
            }

            if (dataBuf.Length == 0)
            {
                label_Display.Text += "Please input JLB frame";
                return;
            }

            label_Display.Text += JLB_FRAME_Check.check(dataBuf);
            return;
        }

        private static byte[] hexStringToArray(string srcStr)
        {
            string[] subStr = srcStr.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            string strWhole = "";

            for (int i = 0; i < subStr.Length; i++)
            {
                if (subStr[i].EndsWith(":"))
                    continue;
                if (subStr[i].Length % 2 != 0)
                    strWhole += "0";

                strWhole += subStr[i];
            }

            byte[] hexarray = new byte[strWhole.Length / 2];

            for (int i = 0; i < strWhole.Length / 2; i++)
            {
                try
                {
                    hexarray[i] = byte.Parse(strWhole.Substring(2 * i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                }
                catch (Exception)
                {
                    return null;
                }
            }
            return hexarray;

        }

    //------------------------------------------------------------
    }
}
